<?php

namespace addons\shopro\controller\commission;

use app\admin\model\shopro\commission\Level;
use app\admin\model\shopro\Config as ShoproConfig;
use app\admin\model\shopro\Performance;
use app\admin\model\shopro\user\Bonus;
use app\admin\model\shopro\user\WalletLog as UserWalletLogModel;
use think\Db;
use app\admin\model\shopro\user\User as UserModel;
use app\admin\model\shopro\commission\Agent as AgentModel;
use app\admin\model\shopro\goods\Goods as GoodsModel;
use addons\shopro\service\Wallet;


class Agent extends Commission
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    // 分销商详情
    public function index()
    {
        $status = $this->service->getAgentStatus(true);

        $condition = [
            'type' => '',
            'value' => ''
        ];

        switch ($status) {
            case AgentModel::AGENT_STATUS_NULL:
                $condition = $this->service->config->getBecomeAgentEvent();
                if ($condition['type'] === 'goods') {
                    $condition['value'] = GoodsModel::show()->whereIn('id', $condition['value'])->select();
                }
                $this->error('', $condition, 100);
                break;
            case AgentModel::AGENT_STATUS_NEEDINFO:
                $this->error('待完善信息,请补充您的资料后提交审核', $condition, 103);
                break;
            case AgentModel::AGENT_STATUS_PENDING:
                $this->error('正在审核中,请耐心等候结果', $condition, 104);
                break;
            case AgentModel::AGENT_STATUS_REJECT:
                $agentFormStatus = $this->service->config->isAgentApplyForm();
                if ($agentFormStatus) {
                    $this->error('抱歉!您的申请信息未通过,请尝试修改后重新提交', $condition, 105);
                } else {
                    $this->error('抱歉!您的申请未通过,请尝试重新申请', $condition, 106);
                }
                break;
            case AgentModel::AGENT_STATUS_FREEZE:
                $this->error('抱歉!您的账户已被冻结,如有疑问请联系客服', $condition, 107);
                break;
        }
        $data = $this->service->agent;

        $this->success('分销商信息', $data);
    }

    // 分销商完善个人信息
    public function form()
    {
        if (!$this->service->config->isAgentApplyForm()) {
            $this->error('未开启分销商申请');
        }

        $agentForm = $this->service->config->getAgentForm();
        $protocol = $this->service->config->getApplyProtocol();
        $applyInfo = $this->service->agent->apply_info ?? [];

        $config = [
            'form' => $agentForm['content'],
            'status' => $this->service->getAgentStatus(true),
            'background' => $agentForm['background_image'],
            'protocol' => $protocol,
            'applyInfo' => $applyInfo
        ];

        $this->success("", $config);
    }

    // 申请分销商/完善资料
    public function apply()
    {
        if (!$this->service->config->isAgentApplyForm()) {
            $this->error('未开启分销商申请');
        }
        $status = $this->service->getAgentStatus(true);

        if (!in_array($status, [AgentModel::AGENT_STATUS_NEEDINFO, AgentModel::AGENT_STATUS_REJECT, AgentModel::AGENT_STATUS_NORMAL])) {
            $this->error('您暂时不能申请');
        }
        Db::transaction(function () use ($status) {
            $data = $this->request->param('data/a');
            // 过滤无效表单字段数据
            $config = $this->service->config->getAgentForm();
            $form = (array)$config['content'];
            $data = array_column($data, 'value', 'name');

            foreach ($form as &$item) {
                if (!empty($data[$item['name']])) {
                    $item['value'] = $data[$item['name']];
                } else {
                    $this->error($item['name'] . '不能为空');
                }
            }
            if ($status === AgentModel::AGENT_STATUS_NEEDINFO) {
                $this->service->createNewAgent('', $form);
            } else {
                // 重置为审核中
                if ($status === AgentModel::AGENT_STATUS_REJECT) {
                    $this->service->agent->status = AgentModel::AGENT_STATUS_PENDING;
                }
                // 更新分销商信息
                $this->service->agent->apply_info = $form;
                $this->service->agent->save();
            }
        });


        $this->success('提交成功');
    }

    // 我的团队
    public function team()
    {
        $agentId = $this->service->user->id;
        $where=["nl_user.status"=>"normal"];
        if(!empty($this->request->param("level"))){
            $where=["agent_level.level"=>$this->request->param("level")];
        }
        $data = UserModel::field("nl_user.*")->where('parent_user_id', $agentId)
            ->where($where)
            ->with(['agent' => function ($query) {
                return $query->with('level_info');
            }])
            ->order("nl_user.createtime","desc")
            ->join("shopro_commission_agent agent_level","agent_level.user_id=nl_user.id","left")
            ->paginate($this->request->param('list_rows', 8))
            ->each(function ($v){
                $performance=Performance::where(["user_id"=>$v["id"]])
                    ->whereTime("createtime","last month")
                    ->field("sum(money) money")
                    ->where("level > 2")
                    ->buildSql();
                $list = Performance::where(["user_id"=>$v["id"]])
                    ->field("sum(money) money")
                    ->whereTime("createtime","month")
                    ->where("level > 2")
                    ->union([$performance], true)
                    ->select();
                $v["lastMonthPerformance"]=$list[1]["money"]??0;//上月业绩
                $v["monthPerformance"]=$list[0]["money"]??0;//本月业绩
            });

        $this->success("", $data);
    }

    // 佣金转余额
    public function transfer()
    {
        $amount = $this->request->param('amount');
        if ($amount <= 0) {
            $this->error('请输入正确的金额');
        }
        Db::transaction(function () use ($amount) {
            $user = auth_user();
            Wallet::change($user, 'commission', -$amount, 'transfer_to_money');
            Wallet::change($user, 'money', $amount, 'transfer_by_commission');
        });
        $this->success('');
    }
    //新增 lina start
    //获取所有等级接口
    public function getLevel()
    {
        $list=Level::field("level,name")->select();
        $this->success("", $list);
    }

    //按月统计门店奖
    public function storePerformance(){
        $year=$this->request->get("year")??date("Y");
        $user=$this->auth->getUser();
        if($user["is_store"]!=1) $this->error("您不能查看门店业绩");
        $list = Db::query("
        SELECT a.month month,ifnull(b.money, 0) money FROM (
      SELECT 1 month UNION ALL 
      SELECT 2 month UNION ALL 
      SELECT 3 month UNION ALL 
      SELECT 4 month UNION ALL
      SELECT 5 month UNION ALL 
      SELECT 6 month UNION ALL 
      SELECT 7 month UNION ALL 
      SELECT 8 month UNION ALL 
      SELECT 9 month UNION ALL
      SELECT 10 month UNION ALL 
      SELECT 11 month UNION ALL 
      SELECT 12 month
    ) a LEFT JOIN
    (
     	SELECT DATE_FORMAT(FROM_UNIXTIME(`createtime`), '%m') AS `month`, sum(money) AS `money`
FROM nl_store_performance where FROM_UNIXTIME(createtime, '%Y')=".$year." and user_id=".$this->auth->id."
GROUP BY `month`
    ) b
    ON a.month=b.month ORDER BY month");
        $this->success("", $list);
    }
    //新增 lina end


    /**
     * 董事分红明细
     * */
    public function dividendLog()
    {
        $user=$this->auth->getUser();
        $configs = ShoproConfig::getConfigs('shop.bonus.bonus_pool', false);
        if(!isset($user["agent"]) || $user["agent"]["level"]!=$configs["level"]) $this->error("您不能查看董事分红明细");
        $list_rows = $this->request->param('list_rows', 10);
        $list=Bonus::field("id,bonus,big_market,small_market,createtime,from_unixtime(createtime, '%Y-%m-%d') createtime_text")
            ->where(["user_id"=>$user["id"]])
            ->order('createtime', 'desc')
            ->paginate($list_rows);
        $this->success("", $list);
    }
}
